'use strict';

const Service = require('egg').Service;
const Mock = require('mockjs');

class MessageService extends Service {
    async getList() {
        const {
            ctx,
            app
        } = this;
        // const {
        //     list
        // } = Mock.mock({
        //     "list|10": [{
        //         title: '@ctitle(4,8)', //标题
        //         name: "@cname", //创建人
        //         create_time: Date.now(), //创建时间
        //         "state|1": [1,0],
        //     }]
        // });
        // list.forEach((item) => {
        //     app.mysql.insert('messages', {
        //         ...item
        //     })
        // })
        
        // return '123'
        const {
            pageSize,
            pageIndex,
            title,
            state
        } = ctx.query;
        let sql = `select * from messages where 1=1 `;
        let sqlCount = `select count(id) as total from messages where 1=1 `;
        if (title) {
            sql += `and title like '%${title}%' `
            sqlCount += `and title like '%${title}%' `
        }
        if (state) {
            sql += `and state = '${state}' `
            sqlCount += `and state = '${state}' `
        }
        //分页
        if (pageSize && pageIndex) {
            sql += `limit ${(pageIndex-1)*pageSize},${pageSize} `
        }
        let res = await app.mysql.query(sql);
        let [{
            total
        }] = await app.mysql.query(sqlCount);
        res.forEach((item, index) => {
            // 序号
            item.number = (pageIndex - 1) * 5 + index + 1;
        })
        return {
            data: res,
            total: total
        }
    }
    // 删除资讯
    async delMessage() {
        const {
            ctx,
            app
        } = this;
        const {
            id
        } = ctx.request.body;
        let res = await app.mysql.delete('messages', {
            id
        });
        return res
    }
    // 修改资讯数据
    async messageChange () {
        const {
            ctx,
            app
        } = this;
        const {
            id,
            state,
        } = ctx.request.body;
        let row = {
            id
        };
        if (state !== undefined) {
            row.state = state
        }
        let res = await app.mysql.update('messages', row);
        return res
    }
    // 添加资讯
    async addMessage() {
        const {ctx,app} = this;
        const {
            title,
            name,
            state,
        } = ctx.request.body;
        let res = await app.mysql.insert('messages',{
            title,
            name,
            state,
            create_time:Date.now(),
        });
        return res
    }
    // 编辑资讯
    async editMessage () {
        const {ctx,app} = this;
        const {
            id,
            name,
            title,
            state,
        } = ctx.request.body;
        let res = await app.mysql.update('messages',{
            id,
            name,
            title,
            state,
        });
        return res
    }
}

module.exports = MessageService;